#include "PlayScene.h"
#include "BackGroundLayer.h"
#include "GamePlayLayer.h"
#include "StatusLayer.h"
#include "Global.h"
#include "GameOverLayer.h"
#include <SimpleAudioEngine.h>

USING_NS_CC;

Scene* PlayScene::createScene()
{
	// 'scene' is an autorelease object
	auto scene = Scene::createWithPhysics();

	auto world = scene->getPhysicsWorld();
	world->setGravity(Vec2(0, -350));

	// 'layer' is an autorelease object
	// world->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

	auto layer = PlayScene::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool PlayScene::init()
{
	//////////////////////////////
	// 1. super init first
	if ( !Layer::init() )
	{
		return false;
	}

	_eventDispatcher->removeAllEventListeners();
	initPhysics();

	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	auto backGroundLayer = BackGroundLayer::create();
	this->addChild(backGroundLayer);
	backGroundLayer->setTag(TagOfLayer::BACKGROUND_LAYER);
	auto gamePlayLayer = GamePlayLayer::create();
	this->addChild(gamePlayLayer);
	gamePlayLayer->setTag(TagOfLayer::GAMEPLAY_LAYER);
	auto statusLayer = StatusLayer::create();
	this->addChild(statusLayer);
	statusLayer->setTag(TagOfLayer::STATUS_LAYER);

	scheduleUpdate();

	auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
	audio->playBackgroundMusic("background.mp3", true);

	return true;
}

bool PlayScene::initPhysics()
{
	auto wallBottom = Node::create();
	this->addChild(wallBottom);
	wallBottom->setAnchorPoint(Vec2::ZERO);
	wallBottom->setPosition(Vec2::ZERO);
	wallBottom->setContentSize(Size(4292967295, Global::g_groundHeight * 2));

	auto material = PHYSICSBODY_MATERIAL_DEFAULT;
	material.density = 1.0f;
	material.restitution = 0.0f;
	material.friction = 0.0f;
	auto body = PhysicsBody::createBox(wallBottom->getContentSize(), material);
	body->setDynamic(false);
	body->setContactTestBitmask(true);
	wallBottom->setPhysicsBody(body);

	auto collisonCoinListener = EventListenerPhysicsContact::create();
	collisonCoinListener->onContactBegin = CC_CALLBACK_1(PlayScene::onCollisionBegin, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(collisonCoinListener, this);

	return true;
}

void PlayScene::update( float dt )
{
	auto gamePlayLayer = (GamePlayLayer*)this->getChildByTag(TagOfLayer::GAMEPLAY_LAYER);

	float eyeX = gamePlayLayer->getEyeX();

	gamePlayLayer->setPosition(-eyeX, Global::g_groundHeight);

	auto backgroundLayer = (BackGroundLayer*)this->getChildByTag(TagOfLayer::BACKGROUND_LAYER);

	backgroundLayer->setPosition(-eyeX, 0);

	for(int i = 0; i < shapesToRemove.size(); i++)
	{
		auto node = shapesToRemove.at(i);
		auto backgroundLayer = (BackGroundLayer*)this->getChildByTag(TagOfLayer::BACKGROUND_LAYER);
		backgroundLayer->removeChild(node);
	}
	shapesToRemove.clear();
}

bool PlayScene::onCollisionBegin(PhysicsContact& contact)
{
	auto shapeA = contact.getShapeA();
	auto nodeA = shapeA->getBody()->getNode();

	auto shapeB = contact.getShapeB();
	auto nodeB = shapeB->getBody()->getNode();

	log("contact a=%d b=%d", nodeA->getTag(), nodeB->getTag());

	if(nodeA->getTag() < 0 || nodeB->getTag() < 0)
	{
		return false;
	}

	if(nodeA->getTag() == TagOfSprite::COIN_SPRITE)
	{
		shapesToRemove.pushBack(nodeA);
		auto statusLayer = (StatusLayer*)this->getChildByTag(TagOfLayer::STATUS_LAYER);
		statusLayer->addCoin(1);
		auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
		audio->playEffect("pickup_coin.mp3");

	}
	else if(nodeB->getTag() == TagOfSprite::COIN_SPRITE)
	{
		shapesToRemove.pushBack(nodeB);
		auto statusLayer = (StatusLayer*)this->getChildByTag(TagOfLayer::STATUS_LAYER);
		statusLayer->addCoin(1);
		auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
		audio->playEffect("pickup_coin.mp3");
	}
	else if(nodeA->getTag() == TagOfSprite::ROCK_SPRITE || nodeB->getTag() == TagOfSprite::ROCK_SPRITE)
	{
		auto director = Director::getInstance();

		director->pause();
		auto gameOverLayer = GameOverLayer::create();

		addChild(gameOverLayer);

		auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
		audio->stopBackgroundMusic();

	}
}
